package array;

/**
 * @ClassName test02
 * @Description TODO
 * @Author hp
 * @Date 2021/7/5 21:10
 * @Version 1.0
 **/
class Solution1 {

    public static void main(String[] args) {
        int[] num = {1,1,1,3,3,2,2,2};
        System.out.println("majorityElement(num) = " + majorityElement(num));
    }

    public static int majorityElement(int[] nums) {
        int cont = 1;
        int m = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (cont == 0) {
                m = nums[i];
            }
            if (m == nums[i]) {
                cont++;
            } else {
                cont--;
            }
        }
        return m;
    }
}